package de.grosjohann.android.feuerwehr.service;

import java.util.List;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import de.grosjohann.android.feuerwehr.IUpdateUIListener;
import de.grosjohann.android.feuerwehr.R;
import de.grosjohann.android.feuerwehr.domain.Fahrzeug;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.util.FileUtil;
import de.grosjohann.android.feuerwehr.util.JSON2ObjectUtil;
import de.grosjohann.android.feuerwehr.webservice.FMSWsFacade;

public class FMSNotificationService
  extends IntentService {
  public static final String FMS_STATUS_AUTHORIZATION = "feuerwehr.FMSStatusAuthorization";
  public static final String FMS_STATUS_KENNUNG = "feuerwehr.FMSStatusKennung";
  public static final String FMS_STATUS_DATA = "feuerwehr.FMSStatusData";
  public static final String FMS_STATUS_ERROR = "feuerwehr.FMSStatusError";

  public static final String HTTP_METHOD = "feuerwehr.HTTP_METHOD";
  public static final String HTTP_METHOD_POST = "POST";
  public static final String HTTP_METHOD_GET = "GET";

  private static final String NAME = "FMSNotificationService";

  public static IUpdateUIListener updateUIListener;

  public FMSNotificationService() {
    super(NAME);
  }

  @Override
  protected void onHandleIntent(Intent newIntent) {

    Log.d(NAME, "handle");

    Context context = getApplicationContext();

    Bundle bundle = newIntent.getExtras();
    String methode =
        (bundle == null) ? "" : (bundle.getString(FMSNotificationService.HTTP_METHOD) == null) ? "" : bundle
            .getString(FMSNotificationService.HTTP_METHOD);

    if (bundle == null || methode.length() == 0 || FMSNotificationService.HTTP_METHOD_GET.equals(methode)) {
      getStatus(context);
    } else if (FMSNotificationService.HTTP_METHOD_POST.equals(bundle.get(FMSNotificationService.HTTP_METHOD))) {
      setStatus(context, bundle);
    }
  }

  private void getStatus(Context context) {
    try {
      /*
       * neue Daten empfangen, alte Daten lesen.
       */
      String json = FMSWsFacade.getFMSStatus(context);
      String lastReceive = FileUtil.readFile(context, FMS_STATUS_DATA);

      /*
       * Empfangene Daten speichern.
       */
      FileUtil.writeFile(context, FMS_STATUS_DATA, json);

      /*
       * Vergleich der Fahrzeugdaten.
       */

      List<Fahrzeug> list = JSON2ObjectUtil.getFahrzeugList(json);
      List<Fahrzeug> oldList = JSON2ObjectUtil.getFahrzeugList(lastReceive);

      if (!list.equals(oldList)) {
        for (int i = 0; i < list.size(); i++) {
          if (!list.get(i).equals(oldList.get(i))) {
            String msg =
                list.get(i).getDescription() + ": " + getString(R.string.lblFMS) + " "
                    + list.get(i).getStatus().getStatus();
            Notify.getInstance().add(context, msg);
          }
        }
      }

    } catch (Exception ex) {
      Log.e(NAME, ex.getMessage(), ex);
    }
  }

  private void setStatus(Context context, Bundle bundle) {
    try {
      String json =
          FMSWsFacade.setFMSStatus(context, bundle.getString(FMSNotificationService.FMS_STATUS_AUTHORIZATION),
              bundle.getString(FMSNotificationService.FMS_STATUS_KENNUNG),
              bundle.getString(FMSNotificationService.FMS_STATUS_DATA));

      FileUtil.writeFile(context, FMS_STATUS_DATA, json);

      if (updateUIListener != null) {
        updateUIListener.showInfo("");
      }
    } catch (Exception ex) {
      Log.e(NAME, ex.getMessage(), ex);
      if (updateUIListener != null) {
        updateUIListener.showError("");
      }

    }
  }
}
